﻿
Partial Class Tools_Update
    Inherits MyUI.PageBase

    Dim a As MyApp.Task.TaskType

    Protected Sub Page_Error(sender As Object, e As EventArgs) Handles Me.Error

        MyApp.WriteProcLog(SF("ERROR TASK: {0}", a.ToString), MyApp.Task.TaskType.DEBUG)
        MyApp.Task.ScheduleTask.Update(a, MyApp.Task.TaskStatusType.READY, Date.UtcNow, "ERROR")

    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim db = mDB.GetDB

        Me.Response.BufferOutput = UI.Page.GetValue("o") = ""
        Me.Server.ScriptTimeout = 60 * 60 * 24 '36000

        Me.Response.Write("<pre>")

        Dim OncedayNight = Now.Hour >= 2 And Now.Hour <= 6

        '----------------------------------------------------
        'GESTIONE TASK
        '----------------------------------------------------
        a = UI.Page.GetValueInt("a")

        Dim s = db.TAB.Schedule_Task.GetByPK(a)

        If s IsNot Nothing Then
            If s.Status_Id <> 0 Then
                MyApp.WriteProcLog(SF("TASK IN PROGRESS: {0}", a.ToString), MyApp.Task.TaskType.DEBUG)
                If UI.Page.GetValue("i") = "" Then
                    MyApp.WriteProcLog(SF("TASK EXIT: {0}", a.ToString), MyApp.Task.TaskType.DEBUG)
                    Exit Sub
                Else
                    MyApp.WriteProcLog(SF("TASK CONTUNUE: {0}", a.ToString), MyApp.Task.TaskType.DEBUG)
                End If
            End If
        End If

        'HttpContext.Current.Items("CURRENT_TASK") = a
        MyApp.Task.ScheduleTask.Update(a, MyApp.Task.TaskStatusType.RUNNING, Date.UtcNow, "")

        '----------------------------------------------------
        ' TASK
        '----------------------------------------------------
        If a = 99 Then


            MyApp.Task.Update.AccountAccountsUpdate(db.VIEW.Account_UserEnabled.ToList(), GloukDB.Account.UpdateAccountMode.FRIENDS)

            ' * NUOVE FUNZONI
            'MyApp.Task.Update.AccountList()
            'MyApp.Task.Update.AccountKlout()


        End If


        If a = 100 Then
            MyApp.WriteProcLog(SF("TASK IN PROGRESS: {0}", a.ToString), MyApp.Task.TaskType.DEBUG)

            'db.ExecuteCommand("bbbbb")

        End If


        ' ogni 20 min
        If a = MyApp.Task.TaskType.UPDATE_ACCOUNTS Or a = MyApp.Task.TaskType.ALL Then

            MyApp.Task.Update.AccountMessagePosts()
            MyApp.Task.Update.AccountMessagesConversations()

            MyApp.Task.Update.AccountPrivatePosts()

            MyApp.Task.Update.AccountShares()
            MyApp.Task.Update.AccountShareDetails()

            MyApp.Task.Update.AccountFavoritePosts()

            MyApp.Task.Update.AccountFollowers()
            MyApp.Task.Update.AccountFriends()

            'IMPORTANTE rifaccio subito gli account per scaricare eventuali account inseriti (foll. folloing, share ecc)
            MyApp.Task.Update.AccountAccounts()

            ' *DEBUG*
            'MyApp.Task.Update.AccountPlaces()

        End If

        ' ogni 15 min
        If a = MyApp.Task.TaskType.UPDATE_POSTS Or a = MyApp.Task.TaskType.ALL Then

            ' scarico i post di: account,  followers, account da condividere
            MyApp.Task.Update.AccountPosts()

            ' metto i post da condividere nel task
            MyApp.Task.Elaborate.AddSharedPostsToTask()

            'scarico rss e metto nel task 
            MyApp.Task.Update.FeedRss()
            MyApp.Task.Elaborate.AddFeedRssToTask()

            ' metto glo autopost nei task
            MyApp.Task.Elaborate.AddAutoPostToTask()

        End If

        ' ogni 5 min
        If a = MyApp.Task.TaskType.UPDATE_QUERY Or a = MyApp.Task.TaskType.ALL Then

            MyApp.Task.Update.QueryPosts()
            MyApp.Task.Update.ProfilePosts()

        End If

        ' ogni 1 min
        If a = MyApp.Task.TaskType.SEND_POSTS Or a = MyApp.Task.TaskType.ALL Then

            MyApp.Task.Send.AccountSchedulePosts()

            MyApp.Task.Send.SendSystemEmail()

        End If

        ' ogni 60 min
        If a = MyApp.Task.TaskType.ELABORATE_DATA Or a = MyApp.Task.TaskType.ALL Then

            ' *DEBUG*
            OncedayNight = True

            MyApp.Task.Elaborate.CreateTrend(OncedayNight)

            MyApp.Task.Elaborate.SetGenderAccounts()
            MyApp.Task.Elaborate.SetBusinessAccounts()
            MyApp.Task.Elaborate.SetStatusAccounts()

            MyApp.Task.Elaborate.AnalysisFollowers(OncedayNight)

        End If

        ' ogni 60 min
        If a = MyApp.Task.TaskType.INDEX_DATA Or a = MyApp.Task.TaskType.ALL Then

            MyApp.Task.Elaborate.CreateIndexFollowersPosts(True)
            MyApp.Task.Elaborate.CategorizeFollowers(True)

        End If



        ' ogni 30 min
        If a = MyApp.Task.TaskType.AUTO_FOLLOW Or a = MyApp.Task.TaskType.ALL Then

            MyApp.Task.Elaborate.AddFollowAccountFromQuery()
            MyApp.Task.Elaborate.AddFollowAccountFromFollowerProfile()

            MyApp.Task.Elaborate.CheckFriendToFollower()

            MyApp.Task.Elaborate.AddUnFollowNotFollower()

            ' PER ORA NON LO USO
            'MyApp.Task.Send.FollowUnfollowAccount()

        End If


        MyApp.Task.ScheduleTask.Update(a, MyApp.Task.TaskStatusType.READY, Date.UtcNow, "OK")


    End Sub



End Class
